//Client Server

   import java.net.*;
   import java.io.*;
   import java.util.*;
   import java.awt.*;
   import javax.swing.*;
   import java.awt.event.*;

    public class clientServer implements ActionListener
   {
   
      Socket outputSocket = null;
      ServerSocket serverSocket = null;
      Socket clientSocket = null;
      PrintWriter out = null;
      JFrame mainFrame;
      JTextArea output;
      JTextField input;
      JButton send;
      JPanel contents;
      LinkedListCli clients;
      String name;
   	
   	   	
       public clientServer(boolean pri, String name)
      {
      	
         this.name = name;  	
      		
         createGUI();
      	
         if(!pri)
         {
            inform();
         }
      	
         runConvo();
      
      }
   	
       public void inform()
      {
      
         Scanner scan1 = new Scanner(System.in);
         System.out.println("Please type the I.P. of the infomation \n server you wish to connect to.");
         String server = scan1.nextLine();
      
         try
         {
            outputSocket = new Socket(server, 4444);
         	
            out = new PrintWriter(outputSocket.getOutputStream(), true);
         	
            Scanner scan2 = new Scanner(System.in);
            System.out.println("Please type the name of and info \n about your communication \n separated by a colon");
            String in = scan2.nextLine();
         
            InetAddress i = null;
         
            try
            {
            
               i = InetAddress.getLocalHost();
            
            }
                catch(Exception e)
               {
               
                  System.out.println(e);
               
               }
         
            if(i != null)
            {
            
               System.out.println(i);
            
            }
           	
            String[] info = new String[3];
         
            info[0] = i.getHostAddress();
            info[1] = in;
            info[2] = "" + port;
         
            for(int j = 0; j < info.length ; j++)
            {
            
               out.println(info[j]);
            
            }
         	
            outputSocket.close();    
         	
            System.out.println("Done...");     	
         	
         }
             catch(IOException e)
            {
            
               System.out.println("Failed to find info server...\n" + e);
            
            }
      }
   	
   
       public void createGUI()
      {
      	
      				
         mainFrame = new JFrame("Instant Messager ClientServer");
         mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         mainFrame.setSize(500,500);
         contents = new JPanel();
      	
         GroupLayout layout = new GroupLayout(contents);			
         contents.setLayout(layout);
      	
         layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);
      	
         output = new JTextArea();
         output.setEditable(false);
      	
         input = new JTextField();
      	
         send = new JButton("SEND");
         send.addActionListener(this);
         send.setActionCommand("send");
         send.setEnabled(false);
      				
         layout.setHorizontalGroup(
            layout.createSequentialGroup()
            .addComponent(output)
            .addComponent(input)
            .addComponent(send)
            );
      		
         layout.setVerticalGroup(
            layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(output)
            .addComponent(input)
            .addComponent(send))
            );
      		
         mainFrame.add(contents);
         mainFrame.setVisible(true);
      	
      }
   	
      int port = 4447;
   	
       public void runConvo()
      {
      	
         clients = new LinkedListCli();      
         boolean listening = true;   	
      	   	
         try
         {
            serverSocket = new ServerSocket(port);
            serverSocket.setSoTimeout(100);      
         	
            while(listening)
            {     	
            	
               try
               {
               
                  System.out.println("Listening");
               
                  if(!clients.isEmpty())
                  { 
                  	
                     System.out.println("Receiving");              
                  	   	
                     send.setEnabled(true);
                  
                     ClientObj[] list = clients.pop();
                  
                     for(int i = 0; i < list.length; i++)
                     {
                     	
                        System.out.println(i);
                     	
                        String temp = recieveMessage(list[i]);
                     
                        if(!temp.equals(""))
                        {
                        
                           output.append(temp + "\n");
                        
                           for(int j = 0; j < list.length; j++)
                           {
                           
                              if(j != i)
                              {
                              
                                 sendMessage(list[j], temp);
                              
                              }
                           
                           }
                        
                        }
                     
                     }
                              
                  }
                  
                  clients.add(serverSocket.accept(), "Name");
               	
               }
                   catch(IOException e)
                  {
                  
                     System.out.println(e);
                  
                  }
            
            }
         }
             catch(IOException e)
            {
             
               System.out.println(e);
                        
            }
      
      }
   	
       public void sendMessage(ClientObj person,String message) 
      {
      			
         try
         {		
               
            PrintWriter outCon = new PrintWriter(person.client.getOutputStream(), true);
         
            outCon.println(message);
                     
         }
             catch(IOException e)
            {
            
               System.out.println(e);
            
            } 		
      	  
      }
   	
       public String recieveMessage(ClientObj person)
      {
      	
         try
         {
         		
            BufferedReader in = new BufferedReader(new InputStreamReader(person.client.getInputStream()));
         	
            if(in.ready())
            {
            
               String temp;
            	     	
               if(!((temp = in.readLine()).equals("")))
               {
               
                  return temp;      	
                 
               }
            
            }
            else
            {
            
               return "";
            
            }
         
         }
             catch(IOException e)
            {
            
               System.out.println(e);
            
            }
      		
         return "";
      
      }
   	
       public void actionPerformed(ActionEvent e)
      {
      
         if(e.getActionCommand().equals("send"))
         {
         	
            System.out.println("Sending");     	
         	
            String temp = name + " : " + input.getText();
         	
            if(!temp.equals(""))
            {
            
               output.append(temp + "\n");
            	
               ClientObj[] list = clients.pop();
            	
               for(int i = 0; i < list.length; i++)
               {
               
                  sendMessage(list[i], temp);
               
               }
            	
               input.setText("");
            
            }
         				
         }
      
      
      }
   
   }